package com.wjt.homestayms.controller;

import com.wjt.homestayms.common.PageRequest;
import com.wjt.homestayms.common.PageResult;
import com.wjt.homestayms.common.Result;
import com.wjt.homestayms.pojo.Orderinfo;
import com.wjt.homestayms.pojo.Room;
import com.wjt.homestayms.pojo.Tenant;
import com.wjt.homestayms.service.IOrderinfoService;
import com.wjt.homestayms.service.IRoomService;
import com.wjt.homestayms.service.IStoreService;
import com.wjt.homestayms.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class RoomController {
    @Autowired
    IRoomService  roomService;
    @Autowired
    IStoreService storeService;
    @Autowired
    IOrderinfoService orderinfoService;
    @Autowired
    ITenantService tenantService;

    @GetMapping("/getRoomList")
    @ResponseBody
    public Map<String,Object> getRoomList(PageRequest pageRequest,Integer id,Integer rNum){
        Map<String, Object> map = new HashMap<>();
        PageResult resultPage = null;
        if (id != null) {
            resultPage = roomService.getResultPage(pageRequest, id);
        }else if(rNum != null){
            resultPage = roomService.getResultPageByRnum(pageRequest, rNum);
        }
        return Result.queryResult(map,resultPage);
    }

    @GetMapping("/getRoomByRid")
    @ResponseBody
    public Map<String,Object> getRoomByRid(Integer rId) {
        Map<String, Object> map = new HashMap<>();
        Room room = roomService.getRoomById(rId);
        if(room != null){
            map.put("code",200);
            map.put("status",true);
            map.put("result",room);
        }else{
            map.put("code",500);
            map.put("status",false);
            map.put("result",null);
        }
        return map;
    }

    @PostMapping("addRoom")
    @ResponseBody
    public Map<String,Object> getRoomList(Room room){
        Map<String,Object> map = new HashMap<>();
        if(room.getrNum() > 0) {
           if(roomService.addRoom(room)){
               map.put("code",200);
               map.put("status",true);
           }else {
               map.put("code",500);
               map.put("status",false);
           }
           return map;
        }
        map.put("code",500);
        map.put("status",false);
        return map;
    }

    @PostMapping("updateRoom")
    @ResponseBody
    public Map<String,Object> updateRoom(Room room){
        Map<String,Object> map = new HashMap<>();
        if(room.getrId() > 0) {
            if(roomService.updateRoom(room)){
                map.put("code",200);
                map.put("status",true);
            }else {
                map.put("code",500);
                map.put("status",false);
            }
            return map;
        }

        map.put("code",500);
        map.put("status",false);
        return map;
    }

    @GetMapping("deleteRoom")
    @ResponseBody
    public Map<String,Object> updateRoom(Integer rId) {
        Map<String, Object> map = new HashMap<>();
        if(rId > 0){
            if(roomService.deleteRoomByRid(rId)){
                map.put("code",200);
                map.put("status",true);
            }else{
                map.put("code",500);
                map.put("status",false);
            }
            return  map;
        }
        map.put("code",500);
        map.put("status",false);
        return  map;
    }

    @GetMapping("/getRoomListByRnum")
    @ResponseBody
    public Map<String,Object> getRoomListByRnum(PageRequest pageRequest,Integer rNum){
        Map<String,Object> map = new HashMap<>();
        if(pageRequest.getPageNum() >=1 || pageRequest.getPageSize() > 0 || rNum >0 ){
            PageResult resultPage = roomService.getResultPageByRnum(pageRequest, rNum);

            Integer sId = roomService.getSidByRnum(rNum);
            String store = storeService.getStoreName(sId);
            map.put("code",200);
            if(resultPage != null) {
                map.put("status", true);
                map.put("store",store);
                map.put("result", resultPage);
            }else{
                map.put("status", false);
                map.put("store",null);
                map.put("result", null);
            }
            return map;
        }
        map.put("code",500);
        map.put("status",false);
        map.put("store",null);
        map.put("result",null);
        return  map;
    }


    /**
     * 实现入住功能
     * @param tName
     * @param idNum
     * @param phoneNum
     * @param id
     * @param rNum
     * @param day
     * @param time
     * @param deposit
     * @param rId
     * @return
     */
    @PostMapping("/ruzhu")
    @ResponseBody
    public Map<String,Object> ruzhu(@RequestParam("tName")String tName,
                                    @RequestParam("idNum")String idNum,
                                    @RequestParam("phoneNum")String phoneNum,
                                    @RequestParam("id")Integer id,
                                    @RequestParam("rNum")Integer rNum,
                                    @RequestParam("day")Integer day,
                                    @RequestParam("time") Date time,
                                    @RequestParam("deposit")Integer deposit,
                                    @RequestParam("rId")Integer rId) {
        //修改房间信息
        boolean f1 = roomService.modifyStatus(rId,"住满");
        //增加顾客信息
        Tenant tenant = new Tenant(null,tName,idNum,phoneNum,id);
        boolean f2 = tenantService.addTenant(tenant);
        //增加订单信息
        Room room = roomService.getRoomById(rId);
        Integer price = room.getrPrice() * day;
        Orderinfo orderinfo = new Orderinfo(null,rNum,price,tName,time,id,deposit,day,null);
        int row = orderinfoService.insertIgnoreNull(orderinfo);
        Map<String, Object> map = new HashMap<>();
        //判断操作是否全部实现成功。
        if(f1 && f2 && row > 0){
            map.put("code",200);
            map.put("status",true);
        }else{
            map.put("code",500);
            map.put("status",false);
        }
        return  map;
    }

    /**
     * 腾空房间
     * @param rId
     * @return
     */
    @PostMapping("/tengkong")
    @ResponseBody
    public Map<String,Object> tengkong(@RequestParam("rId")Integer rId){
        //修改房间信息
        boolean f1 = roomService.modifyStatus(rId,"空闲");
        Map<String, Object> map = new HashMap<>();
        //判断操作是否实现成功。
        if(f1){
            map.put("code",200);
            map.put("status",true);
        }else{
            map.put("code",500);
            map.put("status",false);
        }
        return  map;
    }



}
